<div class="panel panel-default panel-intro">
    <div class="panel-heading">
        <h3 class="panel-title">库存盘点</h3>
    </div>
    <div class="panel-body">
        <div class="row">
            <div class="col-md-12">
                <div class="alert alert-info">
                    <i class="fa fa-info-circle"></i> 请核对实际库存数量，如有差异请修改数量并填写备注。
                </div>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-12">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>物料</th>
                            <th>仓库</th>
                            <th>系统库存</th>
                            <th>实际库存</th>
                            <th>差异</th>
                            <th>备注</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach $stockList as $stock}
                        <tr>
                            <td>{$stock.material.material_name} ({$stock.material.material_code})</td>
                            <td>{$stock.warehouse.warehouse_name}</td>
                            <td>{$stock.quantity}</td>
                            <td>
                                <input type="number" class="form-control actual-quantity" 
                                       data-stock-id="{$stock.id}" 
                                       data-system-quantity="{$stock.quantity}"
                                       value="{$stock.quantity}" 
                                       min="0" step="0.01" style="width: 100px;">
                            </td>
                            <td>
                                <span class="difference" data-stock-id="{$stock.id}">0</span>
                            </td>
                            <td>
                                <input type="text" class="form-control check-remark" 
                                       data-stock-id="{$stock.id}" 
                                       placeholder="盘点备注" style="width: 150px;">
                            </td>
                            <td>
                                <button type="button" class="btn btn-primary btn-sm check-submit" 
                                        data-stock-id="{$stock.id}">
                                    <i class="fa fa-check"></i> 确认
                                </button>
                            </td>
                        </tr>
                        {/foreach}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script>
$(function() {
    // 计算差异
    $('.actual-quantity').on('input', function() {
        var stockId = $(this).data('stock-id');
        var systemQuantity = parseFloat($(this).data('system-quantity'));
        var actualQuantity = parseFloat($(this).val()) || 0;
        var difference = actualQuantity - systemQuantity;
        
        $('.difference[data-stock-id="' + stockId + '"]').text(difference.toFixed(2));
        
        if (difference > 0) {
            $('.difference[data-stock-id="' + stockId + '"]').addClass('text-success').removeClass('text-danger');
        } else if (difference < 0) {
            $('.difference[data-stock-id="' + stockId + '"]').addClass('text-danger').removeClass('text-success');
        } else {
            $('.difference[data-stock-id="' + stockId + '"]').removeClass('text-success text-danger');
        }
    });
    
    // 提交盘点
    $('.check-submit').click(function() {
        var stockId = $(this).data('stock-id');
        var actualQuantity = parseFloat($('.actual-quantity[data-stock-id="' + stockId + '"]').val());
        var remark = $('.check-remark[data-stock-id="' + stockId + '"]').val();
        
        if (isNaN(actualQuantity) || actualQuantity < 0) {
            Toastr.error('请输入有效的实际库存数量');
            return;
        }
        
        $.post('manufacture/stock/check', {
            stock_id: stockId,
            actual_quantity: actualQuantity,
            remark: remark
        }, function(data) {
            if (data.code === 1) {
                Toastr.success('盘点完成');
                // 更新系统库存显示
                $('.actual-quantity[data-stock-id="' + stockId + '"]').data('system-quantity', actualQuantity);
                $('.difference[data-stock-id="' + stockId + '"]').text('0').removeClass('text-success text-danger');
            } else {
                Toastr.error(data.msg || '盘点失败');
            }
        });
    });
});
</script>
